var socket_io = require('socket.io')
var chat = require('./methods')
var socketio = {}
var room_list = {}
// 获取io
socketio.getSocketio = function(server) {
	var io = socket_io.listen(server)
	io.sockets.on('connection', function(socket) {
		socket.emit('room_list_out', { room_list: room_list }) 
		socket.on('create_room', function(data) {
			var data = {},id = new Date().getTime()
			data.id = id
			data.name = '房间' + (Object.keys(room_list).length + 1)
			data.users = []
			data.users.push(socket.id)
			room_list[id] = data
			socket.broadcast.emit('room_list_out', { room_list: room_list }) 
		})

		socket.on('join_room', function(data) {
			 socket.join(data);
			 chat.join(socket,data.data)
		})
		socket.on('setMsg',function(data){
			chat.msg(socket,data.data)
		})
		socket.on('room_list_in', function(data) {
			socket.emit('room_list_out', { data: data }) 
		})
		socket.on('strokes_in', function(data) {
			socket.emit('strokes_out', { data: data }) 
			socket.broadcast.emit('strokes_out', { data: data }) // 发送给其它客户端
			// io.sockets.socket().emit('dsafdsfs', data);
		})
	})
}

module.exports = socketio
